import React from 'react';
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
import { withRouter, Route } from 'react-router-dom';
import { Tools, Icon, Bubble, Modal } from 'weaver-mobile';
import { RouteLayout } from 'weaver-mobile-page';
import { WrCardList } from '../../../public'
import { Empty } from '../coms'
import New from './new'
const alert = Modal.alert;
const getLabel = Tools.getLabel;
/*
  工作计划
*/
@inject('planViewStore')
@withRouter
@observer
class Plan extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.store = this.props.planViewStore;
  }

  componentWillMount() { }

  render() {
    const prefixClass = 'workrelate-mobile-plan-planview-share-';
    let { planmap, planlist, newLayoutStore, canedit } = this.store;
    return (
      <div >
        <div className="workrelate-mobile-plan-planview-planin">
          <WrCardList
            title={getLabel('18170', '工作计划')}
            dataMapList={planmap.list ? planmap.list : []}
            dataList={toJS(planlist)}
            showAdd={canedit}
            showDelete={canedit}
            showCheckBox={canedit}
            onAdd={this.onAdd}
            onDelete={this.onPlanDelete}
            dropList={this.getPlanDropList()}
            emptyComponent={<Empty title={getLabel(83553, '暂无数据')}></Empty>}
            onRowClick={this.onRowClick}
          />
        </div>
      </div>
    );
  }
  //创建任务
  addTask = (record, index) => {

    if (!record.id) {
      this.saveDetail(record).then(res => {
        this.store.planlist[index].id = res.plandetailid
        this.store.addTask(record.name, res.plandetailid, record.begindate2 ? record.begindate2 : record.begindate1, record.enddate2 ? record.enddate2 : record.enddate1).then(res=>{
          if(res.api_status){
            this.openTaskDetail(res.taskId)
          }
        })
      })
    } else {
      this.store.addTask(record.name, record.id, record.begindate2 ? record.begindate2 : record.begindate1, record.enddate2 ? record.enddate2 : record.enddate1).then(res=>{
        if(res.api_status){
          this.openTaskDetail(res.taskId)
        }
      })
    }
  }
  openTaskDetail=(id)=>{
    this.props.history.push(`/taskHome/taskDetail/${id}`)
  }

  onPlanDelete = datas => {
    this.store.planlist = datas;
  };

  onAdd = () => {
    // this.props.history.push({ pathname:'/planHome/planView/new',state:{type : 'plan' } })
    //this.store.recordIndexScrollHeight()
    this.props.history.push({ pathname: this.store.newLayoutStore.path, search: 'plan' })
  };
  //新增一行数据
  addNew = (index) => {
    //this.store.recordIndexScrollHeight()
    this.props.history.push({ pathname: this.store.newLayoutStore.path, search: 'plan&' + index })
  }
  //单个删除
  singleDelete = () => {
    return (index) => {
      this.alertModal(() => {
        if (this.store.planlist[index]) {
          this.store.planlist.splice(index, 1)
        }
      }, getLabel('509113','确认删除此报告吗？') )
    }
  }
  //获取计划内下拉框
  getPlanDropList = () => {
    let { planlist, canedit } = this.store
    let _ = this
    let drops = []
    canedit && drops.push({
      title: getLabel('509025', '新增一行数据'),
      func: (index) => {
        _.addNew(index + 1)
      }
    })
    canedit && drops.push({
      title: getLabel('16336', '创建任务'),
      func: (index) => {
        let record = planlist[index]
        this.alertModal(() => {
          this.addTask(record, index)
        }, getLabel('509023', '确定创建任务？'))

      }
    })
    drops.push({
      title: getLabel('382624', '查看明细'),
      func: _.getDetailViewFunc(planlist)
    })

    if (canedit) {
      drops.push({
        key:'delete',
        title: getLabel(91, '删除'),
        func: _.singleDelete()
      })
    }
    return drops
  }

  saveDetail = (data) => {
    let params = {
      operation: 'save_detail',
      resourceid: this.store.resourceid, dataType: 2,
      type1: this.store.type1,
      detailData: JSON.stringify([data])
    };
    return this.store.saveDetailView(params)
  }

  onRowClick = (record, index) => {
    //this.store.recordIndexScrollHeight()
    let noEditFields = []
    let { canedit } = this.store
    this.store.detailId = record.id
    let detailid = this.store.planlist[index].id
    if (!detailid) {
      this.saveDetail(record).then(res => {
        if (res.api_status == 1) {
          detailid = res.plandetailid
          this.store.planlist[index].id = detailid
          this.props.history.push({
            pathname: this.store.detailLayoutStore.path,
            state: { canedit, noEditFields, detailid, init: true, datatype: 2, type1: this.store.type1, resourceid: this.store.resourceid },
            search: `${detailid}&${2}&${this.store.type1}&${this.store.resourceid}&${canedit}&${noEditFields.join('^')}`
          })
        } else {
          Bubble.error(getLabel('508818', '接口调用失败！'), 3);
        }

      })
    } else {
      this.props.history.push({
        pathname: this.store.detailLayoutStore.path,
        state: { canedit, noEditFields, detailid, init: true, datatype: 2, type1: this.store.type1, resourceid: this.store.resourceid },
        search: `${detailid}&${2}&${this.store.type1}&${this.store.resourceid}&${canedit}&${noEditFields.join('^')}`
      })
    }

  }

  //alertModal
  alertModal = (onOk, title) => {
    alert(getLabel('386786', '信息确认'), title, [
      { text: getLabel(31129, '取消'), onPress: () => { } },
      {
        text: getLabel(826, '确定'),
        onPress: () => {
          if (onOk) {
            onOk()
          }
        },
      },
    ])
  }
  getDetailViewFunc = (dataList) => {
    let { canedit } = this.store
    return (index) => {
      //this.store.recordIndexScrollHeight()
      //如果没有id 要先保存 
      let detailid = dataList[index].id
      this.store.detailId = detailid
      if (!detailid) {
        this.saveDetail(dataList[index]).then(res => {
          if (res.api_status == 1) {
            dataList[index].id = res.plandetailid
            detailid = res.plandetailid
            this.props.history.push({
              pathname: this.store.detailLayoutStore.path,
              search: `${detailid}&${2}&${this.store.type1}&${this.store.resourceid}&${canedit}`

            })

          } else {
            Bubble.error(getLabel('508818', '接口调用失败！'), 3);
          }

        })
      } else {
        this.props.history.push({
          pathname: this.store.detailLayoutStore.path,
          search: `${detailid}&${2}&${this.store.type1}&${this.store.resourceid}&${canedit}`

        })
      }

    }
  }



}

export default Plan;
